package com.control.dao;

import com.control.model.CookerModelInfo;
import com.control.sqlitemanager.CookerListManager.Cooker_column;
import com.control.sqlitemanager.CookerConfigInfoManager.CookerModelInfo_Column;
import com.control.sqlitemanager.CookerConfigManager.CookerModel_Column;

import android.database.sqlite.SQLiteDatabase;

public final class TableManager {
	
	private SQLiteDatabase db;
	
	private  static final StringBuffer cooker_table = new StringBuffer();
	
	public TableManager(SQLiteDatabase db){
		this.db = db;
	}
	
	public static void createTable(SQLiteDatabase db){
		if(db == null){
			return;
		}
		onUpgrade(db);
		db.execSQL(getCookerTable());
		db.execSQL(getCookerModelTable());
		db.execSQL(getCookerModelInfoTable());
	}
	
	public static  void onUpgrade(SQLiteDatabase db){
		if(db == null){
			return;
		}
		db.execSQL("DROP TABLE IF EXISTS " +Cooker_column.cooker);
		db.execSQL("DROP TABLE IF EXISTS " +CookerModel_Column.cooker_model);
		db.execSQL("DROP TABLE IF EXISTS  " + CookerModelInfo_Column.cooker_model_info);
	}
	
	private static String getCookerTable(){
		cooker_table.delete(0, cooker_table.length());
		cooker_table.append("CREATE TABLE "+Cooker_column.cooker);
		cooker_table.append("("+Cooker_column.ID+" INTEGER PRIMARY KEY autoincrement,");
		cooker_table.append(""+Cooker_column.COOKER_CODE+" varchar(50),");
		cooker_table.append(""+Cooker_column.COOKER_NAME+" varchar(50),");
		cooker_table.append(""+Cooker_column.COOKER_IMG_NAME+" varchar(50),");
		cooker_table.append(""+Cooker_column.COOKER_IMG_URL+" varchar(50),");
		cooker_table.append(""+Cooker_column.OVERTIME+" varchar(50),");
		cooker_table.append(""+Cooker_column.POWER_LEVEL+" varchar(50),");
		cooker_table.append(""+Cooker_column.CONTROL_STATUS+" varchar(50),");
		cooker_table.append(""+Cooker_column.WORK_TIME+" varchar(50),");
		cooker_table.append(""+Cooker_column.COOKER_STATUS+" char(2),");
		cooker_table.append(""+Cooker_column.HEATING+" INTEGER,");
		cooker_table.append(""+Cooker_column.KEEPWARM+" INTEGER,");
		cooker_table.append(""+Cooker_column.IP+" varchar(50))");
		return cooker_table.toString();
	}
	
	private static String getCookerModelTable(){
		cooker_table.delete(0, cooker_table.length());
		cooker_table.append("CREATE TABLE "+String.valueOf(CookerModel_Column.cooker_model));
		cooker_table.append("("+CookerModel_Column.id+" INTEGER PRIMARY KEY autoincrement,");
		cooker_table.append(""+CookerModel_Column.name+" varchar(50),");
		cooker_table.append(""+CookerModel_Column.Cooker_id+" varchar(50),");
		cooker_table.append(""+CookerModel_Column.img_url+" varchar(50))");
		return cooker_table.toString();
	}

	
	private static String getCookerModelInfoTable(){
		cooker_table.delete(0, cooker_table.length());
		cooker_table.append("CREATE TABLE "+CookerModelInfo_Column.cooker_model_info);
		cooker_table.append("("+CookerModelInfo_Column.id+" INTEGER PRIMARY KEY autoincrement,");
		cooker_table.append(""+CookerModelInfo_Column.stage+" varchar(50),");
		cooker_table.append(""+CookerModelInfo_Column.time+" varchar(50),");
		cooker_table.append(""+CookerModelInfo_Column.warn+" varchar(50),");
		cooker_table.append(""+CookerModelInfo_Column.cooker_model_id+" INTEGER,");
		cooker_table.append(""+CookerModelInfo_Column.cooker_power+" INTEGER)");
		return cooker_table.toString();
	}
}
